package xcmg.device.web.ratingManager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.ratingManager.ScoreInfoService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.ratingManager.BasisQueryVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

/**
 * Created by wrg on 2019/7/11.
 */
@RestController
@RequestMapping("/scoreInfo")
@Api("评分信息相关入口")
public class ScoreInfoController extends BasicController {
    @Autowired
    private ScoreInfoService scoreInfoService;

    @GetMapping(value = "generateRating")
    @ApiOperation(value = "生成评分")
    @FastMappingInfo(needLogin = true)
    public ActionResult generateRating(@RequestParam("ratingId") String ratingId) throws Exception {
        try{
            FunctionResult functionResult = scoreInfoService.generateRating(ratingId, getUserId(), getUserName(), getOrgId());
            return actionResult(functionResult.getCode(),functionResult.getValue());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取销售完成情况配置列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BasisQueryVO> condition) throws Exception {
        if (condition.getFilters() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }

        PageCommonVO pageCommonVO = scoreInfoService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "generateRanking")
    @ApiOperation(value = "生成排名")
    @FastMappingInfo(needLogin = true)
    public ActionResult generateRanking(@RequestParam("ratingId") String ratingId) throws Exception {
        return actionResult(scoreInfoService.generateRanking(ratingId));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出所有数据")
    public void exportAll(BasisQueryVO condition)throws Exception {
        if (null == condition){
            condition = new BasisQueryVO();
        }
        scoreInfoService.exportAll(condition);
    }

}
